/Users/anhnph/Downloads/Work/Products/Aptos-Cpp-SDK/Accounts/Account.cpp
Line | Count | Source |
1 | | // |
2 | | // Created by Anh NPH on 28/09/2023. |
3 | | // |
4 | | |
5 | | #include "Account.h" |
6 | | #include <cryptopp/xed25519.h> |
7 | | #include <cryptopp/osrng.h> |
8 | | #include "../HDWallet/Utils/Utils.h" |
9 | | #include <cryptopp/cryptlib.h> |
10 | | #include "AuthenticationKey.h" |
11 | | |
12 | | namespace Aptos::Accounts |
13 | | { |
14 | | |
15 | | Account::Account() |
16 | 4 | { |
17 | 4 | CryptoPP::AutoSeededRandomPool rng; |
18 | 4 | CryptoPP::ed25519PrivateKey privateKey; |
19 | 4 | privateKey.GenerateRandom(rng, CryptoPP::g_nullNameValuePairs); |
20 | 4 | CryptoPP::ed25519PublicKey publicKey; |
21 | 4 | privateKey.MakePublicKey(publicKey); |
22 | 4 | CryptoPP::SecByteBlock privateKeyBytes(privateKey.GetPrivateKeyBytePtr(), Utils::Ed25519PrivateKeySeedSizeInBytes); |
23 | 4 | CryptoPP::SecByteBlock publicKeyBytes(publicKey.GetPublicKeyBytePtr(), Utils::Ed25519PublicKeySizeInBytes); |
24 | 4 | m_privateKey = std::make_shared<PrivateKey>(privateKeyBytes); |
25 | 4 | m_publicKey = std::make_shared<PublicKey>(publicKeyBytes); |
26 | 4 | m_accountAddress = std::make_shared<AccountAddress>(AccountAddress::FromKey(publicKeyBytes)); |
27 | 4 | } |
28 | | |
29 | | Account::Account(std::string privateKeyStr, std::string publicKeyStr) |
30 | 1 | { |
31 | 1 | m_privateKey = std::make_shared<PrivateKey>(privateKeyStr); |
32 | 1 | m_publicKey = std::make_shared<PublicKey>(publicKeyStr); |
33 | 1 | m_accountAddress = std::make_shared<AccountAddress>(AccountAddress::FromKey(*m_publicKey)); |
34 | 1 | } |
35 | | |
36 | | Account::Account(const CryptoPP::SecByteBlock &privateKeyBytes, const CryptoPP::SecByteBlock &publicKeyBytes) |
37 | 8 | { |
38 | 8 | m_privateKey = std::make_shared<PrivateKey>(privateKeyBytes); |
39 | 8 | m_publicKey = std::make_shared<PublicKey>(publicKeyBytes); |
40 | 8 | m_accountAddress = std::make_shared<AccountAddress>(AccountAddress::FromKey(*m_publicKey)); |
41 | 8 | } |
42 | | |
43 | | Account Account::LoadKey(std::string privateKeyHex) |
44 | 1 | { |
45 | 1 | PrivateKey privateKey(privateKeyHex); |
46 | 1 | auto publicKey = privateKey.GetPublicKey(); |
47 | 1 | return Account(privateKey.ToString(), publicKey.ToString()); |
48 | 1 | } |
49 | | |
50 | | std::string Account::AuthKey() |
51 | 1 | { |
52 | 1 | auto authkey = AuthenticationKey::FromEd25519PublicKey(m_publicKey->KeyBytes()); |
53 | 1 | return authkey.DerivedAddress(); |
54 | 1 | } |
55 | | |
56 | | bool Account::Verify(const CryptoPP::SecByteBlock &message, Signature signature) |
57 | 2 | { |
58 | 2 | return m_publicKey->Verify(message, signature); |
59 | 2 | } |
60 | | |
61 | | Signature Account::Sign(CryptoPP::SecByteBlock message) |
62 | 3 | { |
63 | 3 | return m_privateKey->Sign(message); |
64 | 3 | } |
65 | | |
66 | | std::shared_ptr<PrivateKey> Account::getPrivateKey() const |
67 | 3 | { |
68 | 3 | return m_privateKey; |
69 | 3 | } |
70 | | |
71 | | std::shared_ptr<PublicKey> Account::getPublicKey() const |
72 | 3 | { |
73 | 3 | return m_publicKey; |
74 | 3 | } |
75 | | |
76 | | std::shared_ptr<AccountAddress> Account::getAccountAddress() const |
77 | 2 | { |
78 | 2 | return m_accountAddress; |
79 | 2 | } |
80 | | |
81 | | } |